struct Num {
    count: i32,
}

impl Num {
    // 方法
    // 不可变借用
    fn read_count(&self) {
        println!("read_count:{}", self.count);
    }
    fn get_count(&self) -> i32 {
        self.count
    }

    // 可变借用
    fn add_count(&mut self) {
        println!("add_count: {} + 2 = {}", self.count, self.count + 2);
        self.count += 2;
    }
    // 移交所有权
    fn free_count(self) {
        println!("free count:{}", self.count);
    }
    // 关联函数
    fn new(&self) -> Self {
        Num {
            count: self.count + 5,
        }
    }
}

fn main() {
    let mut count = Num { count: 10 };
    println!("get_count:{}", count.get_count());
    count.read_count();
    count.add_count();
    count.add_count();
    count.read_count();
    println!("get_count:{}", count.get_count());
    let mut new_count = count.new();
    // count.free_count();
    // count.read_count(); // borrow of moved value
    new_count.read_count();
    new_count.add_count();
    println!("get_count:{}", count.get_count());
    println!("get_new_count:{}", new_count.get_count());
    count.free_count();
    new_count.free_count();
}
